package com.ttnet.beans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

import com.ttnet.bo.FileBo;
import com.ttnet.model.Weather;

@ViewScoped
@ManagedBean(name = "weatherBean")
public class WeatherBean {

	@ManagedProperty(value = "#{fileBo}", name = "fileBo")
	private FileBo fileBo;

	private UploadedFile file;

	private List<Weather> weatherList;
	private String sessionId;

	@PostConstruct
	public void initModel() {

		FacesContext fCtx = FacesContext.getCurrentInstance();
		HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
		sessionId = session.getId();
	}

	public void handleFileUpload(FileUploadEvent event) {
		weatherList = null;
		file = event.getFile();
		// String absolutePath = new java.io.File("").getAbsolutePath();
		// System.out.println("absolutePath : " + absolutePath);
		try {
			List<String> omitStringList = new ArrayList<String>();
			List<String> ignoreStringList = new ArrayList<String>();
			omitStringList.add(" ");
			StringBuffer readFromDatFile = fileBo.readFromDatFile(file.getInputstream(), "Dy", "mo", omitStringList, ignoreStringList, 3);
			fileBo.writeTxtFile(readFromDatFile, sessionId);

		} catch (IOException e) {
			e.printStackTrace();
			file = null;
			FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, event.getFile().getFileName() + " is not uploaded or validated", "");
			FacesContext.getCurrentInstance().addMessage(null, msg);

		}

		FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getFile().getFileName() + " is uploaded and validated", "");
		FacesContext.getCurrentInstance().addMessage(null, msg);

	}

	public void show(ActionEvent event) {
		String file = System.getProperty("user.dir") + "\\" + sessionId + ".txt";
		weatherList = new ArrayList<Weather>();
		try {
			StringBuffer readFromTxtFile = fileBo.readFromTxtFile(file);
			StringTokenizer st = new StringTokenizer(readFromTxtFile.toString(), "\n");
			while (st.hasMoreTokens()) {
				weatherList.add(new Weather(st.nextToken()));
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public UploadedFile getFile() {
		return file;
	}

	public void setFile(UploadedFile file) {
		this.file = file;
	}

	public FileBo getFileBo() {
		return fileBo;
	}

	public void setFileBo(FileBo fileBo) {
		this.fileBo = fileBo;
	}

	public List<Weather> getWeatherList() {
		return weatherList;
	}

	public void setWeatherList(List<Weather> weatherList) {
		this.weatherList = weatherList;
	}

}
